using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace GameWP
{
    public class LoadingComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Game _game;
        private SpriteBatch _spriteBatch;
        private SpriteFont _menuFont, _textFont;
        private Vector2 touchPosition, pretouchPosition;
        private List<Texture2D> _listLoading, _listGuide;
        private Texture2D _bg;
        private Button _loadingText;
        private int _indexLoading, _indexGuide;
        private int _countLoading, _maxcountLoading;
        private bool _isShow;
        public bool IsShow
        {
            get { return _isShow; }
            set { _isShow = value; }
        }

        //Initilation
        public LoadingComponent(Game game, List<Texture2D> listLoading, List<Texture2D> listGuide, Texture2D bg, SpriteFont menuFont, SpriteFont textFont, SpriteBatch spriteBatch)
            : base(game)
        {
            _game = game;
            _menuFont = menuFont;
            _textFont = textFont;
            _spriteBatch = spriteBatch;
            _bg = bg;
            _listLoading = listLoading;
            _listGuide = listGuide;
            _isShow = false;

            _loadingText = new Button("loading.....", textFont, new Vector2(560, 400), Color.Red) { HaveSound = false };
        }

        public void Reset()
        {
            _indexLoading = 0;
            _indexGuide = ManagerGame.rand.Next(0, _listGuide.Count);
            _maxcountLoading = ManagerGame.rand.Next(3,8);
            _countLoading = 0;
        }

        public void CallIsShow()
        {
            Reset();
            _isShow = true;
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
                base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here    
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                _game.Exit();
            //Key input
            TouchCollection touchCollection = TouchPanel.GetState();
            touchPosition = Vector2.Zero;
            if (touchCollection.Count > 0)
            {
                touchPosition = touchCollection[0].Position;
                pretouchPosition = touchPosition;
            }
            _indexLoading++;
            if (_indexLoading >= _listLoading.Count)
            {
                _indexLoading = 0;
                _countLoading++;
                if (_countLoading == _maxcountLoading)
                    _isShow = false;
            }

        }

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin();
            base.Draw(gameTime);
            _spriteBatch.Draw(_bg, Vector2.Zero, Color.White);
            _spriteBatch.Draw(_listLoading[_indexLoading], new Vector2(500, 400), Color.White);
            _loadingText.Draw(_spriteBatch, gameTime);
            _spriteBatch.Draw(_listGuide[_indexGuide], new Vector2(480, 10), Color.White);
            _spriteBatch.End();
        }
    }
}
